package com.example.controller;

import com.example.common.Result;
import com.example.entity.Goods;
import com.example.service.GoodsService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 菜品信息(Goods)表控制层
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    
    @Resource
    private GoodsService goodsService;

    // 信息列表
    @ResponseBody
    @RequestMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer pageNum,
                      @RequestParam(defaultValue = "1") Integer pageSize,
                      HttpServletRequest request) {
        String key = request.getParameter("key");
        String key1 = request.getParameter("key1");
        
        PageInfo<Goods> pageInfo = goodsService.getGoodsList(pageNum, pageSize, key, key1);
        return Result.success(pageInfo);
    }

    // 添加或编辑信息
    @ResponseBody
    @RequestMapping("/add")
    public Result add(Goods goods, HttpServletRequest request) {
        goodsService.saveOrUpdate(goods);
        return Result.success();
    }

    // 是否推荐
    @ResponseBody
    @RequestMapping("/edit")
    public Result edit(Goods goods, HttpServletRequest request) {
        goodsService.updateGoodsRecommendation(goods);
        return Result.success();
    }
    
    // 设置特价
    @ResponseBody
    @RequestMapping("/tj")
    public Result tj(int id, Double tjprice, HttpServletRequest request) {
        goodsService.setSpecialPrice(id, tjprice);
        return Result.success();
    }

    // 查询信息详情
    @ResponseBody
    @RequestMapping("/detail")
    public Result detail(int id, HttpServletRequest request) {
        HashMap<String, Object> res = goodsService.getGoodsDetail(id, request);
        return Result.success(res);
    }

    // 删除信息
    @ResponseBody
    @RequestMapping("/del")
    public Result del(int id, HttpServletRequest request) {
        goodsService.deleteGoods(id);
        return Result.success();
    }

    // 批量删除信息
    @ResponseBody
    @RequestMapping("/deleteAll")
    public Result deleteAll(HttpServletRequest request) {
        String vals = request.getParameter("vals");
        goodsService.batchDeleteGoods(vals);
        return Result.success();
    }
}

